package com.antcloud.demo.antscheduler.handlers;

import com.alipay.antschedulerclient.common.SplitChunkDataResult;
import com.alipay.antschedulerclient.handler.IClusterJobSplitHandler;
import com.alipay.antschedulerclient.model.ClusterJobSplitContext;
import com.alipay.antschedulerclient.model.chunk.RangeChunkData;
import com.alipay.antschedulerclient.model.chunk.ShardingChunkData;

import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author manxing
 * @date 2018/9/2
 */
public class ClusterJobSecondSplitor implements IClusterJobSplitHandler<RangeChunkData> {

    @Override
    public SplitChunkDataResult<RangeChunkData> handle(ClusterJobSplitContext context) {
        ShardingChunkData shardingChunkData = (ShardingChunkData) context.getChunkData();
        SplitChunkDataResult<RangeChunkData> splitChunkDataResult = new SplitChunkDataResult<>();
        ArrayList<RangeChunkData> rangeChunkDatas = new ArrayList<>();
        for (int idx = 0; idx < 100; idx++) {
            rangeChunkDatas.add(new RangeChunkData(shardingChunkData.getShardingRule() + "," + idx,
                String.valueOf(idx * 100), String.valueOf((idx + 1) * 100 - 1)));
        }
        splitChunkDataResult.setChunkDatum(rangeChunkDatas);
        splitChunkDataResult.setSuccess(true);
        return splitChunkDataResult;
    }

    @Override
    public String getName() {
        return "ANTSCHEDULER_CLUSTER_SECOND_SPLITOR";
    }

    @Override
    public ThreadPoolExecutor getThreadPool() {
        return null;
    }
}
